import Vue from 'vue'
import Vuex from 'vuex'
import Session from '../lib/session.js'
import {
	LOGIN_STATUS,
	USER_INFO
} from '../config/session.js'
import { toLogin } from '../lib/utils.js'
Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		token: Session.get(LOGIN_STATUS) || null,
		mer: Session.get(USER_INFO) || null,
		shop: null,
	},
	getters: {
		getToken: state => state.token,
		getMer: state => state.mer,
		getShop: state => state.shop,
	},
	mutations: {
		LOGIN(state, info) {
			state.token = info.token;
			state.mer = info.admin;
			Session.set(LOGIN_STATUS, info.token);
			Session.set(USER_INFO, info.token);
		},
		LOGOUT(state) {
			state.token = null;
			state.admin = null;
			Session.clear(LOGIN_STATUS);
			Session.clear(USER_INFO);
		},
		UPDATE_SHOP(state,data) {
			state.shop = data;
		}
	},
	actions: {
		LOGIN({
			commit
		}, info) {
			commit('LOGIN', info)
		},
		LOGOUT({
			commit
		}) {
			commit('LOGOUT');
			toLogin();
		},
	},
})
